package com.restaurant.springboot.controller;

import com.restaurant.springboot.entity.dto.DishesDTO;
import com.restaurant.springboot.entity.DishesEntity;
import com.restaurant.springboot.service.DishesService;
import com.restaurant.springboot.utils.RespCode;
import com.restaurant.springboot.utils.RespEntity;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

@RestController
@RequestMapping("api/dishes")
public class DishesController {

    @Autowired
    private DishesService dishesService;


    /**
     * 分页获取所有菜品
     * @return
     */
    @GetMapping("")
    public RespEntity getAllDishes(){
        List<DishesEntity> dishesEntityList = dishesService.getAllDishes();
        List<DishesDTO> dishesDTOList = new ArrayList<>();
        for (DishesEntity dishesEntity : dishesEntityList) {
            dishesDTOList.add(new DishesDTO(dishesEntity));
        }
        return new RespEntity(RespCode.SUCCESS, dishesDTOList);
    }

    /**
     * 分页获取所有菜品
     * @return
     */
    @GetMapping("/{pageNum}/{pageSize}")
    public RespEntity getAllDishesOfPage(@PathVariable int pageNum,@PathVariable int pageSize){
        Pageable page = PageRequest.of(pageNum,pageSize);
        return new RespEntity(RespCode.SUCCESS,dishesService.getDishesOfPage(page));
    }

    /**
     * 获取单个菜品的信息
     * @param dishesId
     * @return
     */
    @GetMapping("{dishesId}")
    public RespEntity getDishesById(@PathVariable int dishesId){
        return new RespEntity(RespCode.SUCCESS,new DishesDTO(dishesService.getDishesById(dishesId)));
    }

}
